const app = getApp()
var xrSdk     = require('../../../vendor/hope-sdk/index.js');
var xrRequest = require('../../../vendor/hope-sdk/core/request.js');
var config = require('../../../config.js');
const util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    webURL: config.service.imageUrl,
    //上传地址
    serverUrl: config.service.upload,

    id:0,
    description:'',
    car:null,
    shop:null,
    service:null,
    remark:'',
    current:0,
    imageData: []       //最多上传3张图片
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    var data =  JSON.parse(decodeURIComponent(options.data));
    that.init(data);
  },
  init:function(data){
    this.setData({
      car:data.car,
      shop:data.shop,
      service:data.service,
      remark:data.remark,
      id:data.id
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  change:function(e){
    console.log(e);
    this.setData({
      current:e.detail.index
    });
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  sureClick:function(e){
    if(this.data.current < 1){
      util.toast('请选择星级');
      return;
    }
    var that = this;
    wx.showModal({
      title: '确定提交吗',
      content: '确定提交评论吗？',
      success(res) {
        if (res.confirm) {
          that.sureHandler();
          console.log('用户点确定了')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });  
  },
  sureHandler:function(order_id){
    var images = this.data.imageData.join(',');
    var data = {
      id      : this.data.id,
      grade   : this.data.current,
      images  : images,
      evaluate: this.data.description
    };
    var that = this;
    xrRequest.request(app,{
      url: config.service.rescue_evaluate,
      login: true,
      data: data,
      success: function (res) {
        if (res.data.code == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          });          
          wx.navigateTo({
            url: "../my/my"
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res,
          icon: 'none',
        })
       },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  result: function(e) {
    console.log(e)
    this.setData({
      imageData: e.detail.imgArr
    })
  },
  remove: function(e) {
    //移除图片
    console.log(e)
    let index = e.detail.index
  },
  descriptionInput:function(e){
    this.setData({
      description: e.detail.value
    });
  }
})